package com.wierd0games.UnderLD.main;

import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Random;

import com.wierd0games.UnderLD.graphics.Draw;
import com.wierd0games.UnderLD.graphics.Sprite;
import com.wierd0games.UnderLD.graphics.SpriteSheet;
import com.wierd0games.UnderLD.input.Keyboard;
import com.wierd0games.UnderLD.mobs.Frog;
import com.wierd0games.UnderLD.mobs.Healthbar;

public class Main extends Applet implements Runnable {
	private static final long serialVersionUID=1L;

	private BufferStrategy bufferStrategy;
	private Thread thread;
	private Draw draw;
	private Keyboard keyboard;
	private Canvas drawArea;
	private boolean running=true;
	private final long TIME_BETWEEN_UPDATES=1000000000/60;
	private long lastNanoTime=0;
	private long currentNanoTime=0;
	private long lastMilliTime=0;
	private int updates=0;
	private int frames=0;

	private final int WIDTH=300;
	private final int HEIGHT=WIDTH*9/16;
	private final int SCALE=3;

	private BufferedImage image=new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
	int[] pixels=((DataBufferInt)image.getRaster().getDataBuffer()).getData();

	Frog frog;
	private int[] sideCollisions=new int[0];
	private int[] verticalCollisions=new int[0];
	private final int WATER_LEVEL=1600;

	private Generator generator;
	private Healthbar healthbar;
	private int score=0;
	private int time=60;
	private int lastScore=0;
	private int lastTimeAlive=0;

	private int room=0;
	private int counter=0;
	private Sprite openingScreenToDraw=new Sprite(1, 1, 300, SpriteSheet.backgrounds);
	Random random=new Random();

	public void init() {
		keyboard=new Keyboard();
		draw=new Draw(WIDTH, HEIGHT, pixels);
		requestFocus();
		frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
		generator=new Generator(WATER_LEVEL);
		healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);

		lastNanoTime=System.nanoTime();
		lastMilliTime=System.currentTimeMillis();
		thread=new Thread(this);
		drawArea=new Canvas();
		setIgnoreRepaint(true);
		thread.start();
		this.setSize(WIDTH*SCALE, HEIGHT*SCALE);
	}

	public void destroy() {
		running=false;
		super.destroy();
	}

	public void render() {
		for (int i=0; i<pixels.length; i++) {//sets the background color to white so that the clouds don't have holes in them
			pixels[i]=-1;
		}

		if (room==0) {
			draw.drawSpriteToScreen(0, 0, openingScreenToDraw);
		}

		if (room==1) {
			draw.drawSpriteToScreen(0, 0, openingScreenToDraw);
		}

		if (room==2) {
			draw.drawSpriteToScreen(0, Math.min(0, -1*(int)frog.y+WATER_LEVEL-HEIGHT/2-2), Sprite.sky);
			draw.drawSpriteToScreen(0, Math.max(0, -1*(int)frog.y+HEIGHT+WATER_LEVEL-HEIGHT/2-2), Sprite.water);

			frog.drawSelf(draw);
			generator.drawBlocks(draw);
			healthbar.drawSelf(draw, score, time);
		}
		if (room==3) {
			draw.drawSpriteToScreen(0, 0, Sprite.youStarved);
			draw.drawNumberToScreen(80, 64, lastScore);
			draw.drawNumberToScreen(117, 79, lastTimeAlive);
		}
		if (room==4) {
			draw.drawSpriteToScreen(0, 0, Sprite.youDrowned);
			draw.drawNumberToScreen(80, 64, lastScore);
			draw.drawNumberToScreen(117, 79, lastTimeAlive);
		}
		if (room==5) {
			draw.drawSpriteToScreen(0, 0, Sprite.outOfTime);
			draw.drawNumberToScreen(80, 64, lastScore);
			draw.drawNumberToScreen(117, 79, lastTimeAlive);
		}
		if (room==6) {
			draw.drawSpriteToScreen(0, 0, Sprite.youWereEaten);
			draw.drawNumberToScreen(80, 64, lastScore);
			draw.drawNumberToScreen(117, 79, lastTimeAlive);
		}

		if (bufferStrategy.getDrawGraphics()!=null) {
			Graphics g=bufferStrategy.getDrawGraphics();
			g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
			g.dispose();
			bufferStrategy.show();
		}
	}

	public void update() {
		if (room==0) {
			if (++counter>300) {
				room=1;
				counter=-60;
				openingScreenToDraw=Sprite.titleScreen;
			}
			else {
				if (counter>120&&counter<280) {
					for (int i=0; i<openingScreenToDraw.pixels.length; i++) {
						if (openingScreenToDraw.pixels[i]!=0) {
							if (random.nextInt((300-counter-20))==0) {
								openingScreenToDraw.pixels[i]=0;
							}
						}
					}
				}
			}
		}

		if (room==1) {
			keyboard.update();
			if (++counter>=0) {
				if (counter>40) {
					counter=0;
					if (openingScreenToDraw==Sprite.titleScreen) {
						openingScreenToDraw=Sprite.titleScreen2;
					}
					else {
						openingScreenToDraw=Sprite.titleScreen;
					}
				}

				if (keyboard.keysPressed[10]) {
					room=2;
					counter=0;
				}
			}
		}

		if (room==2) {
			if (++counter>=60) {
				counter=0;
				time--;
			}
			keyboard.update();
			for (int i=0; i<keyboard.keys.length; i++) {
				if (keyboard.keysPressed[i]) {
					System.out.println("Key "+i+" Pressed");
				}
			}
			int[] returned=generator.update((int)frog.x, (int)frog.y);
			int fliesToEat=returned[0];
			int chestsToCollect=returned[1];
			score+=fliesToEat*10+chestsToCollect*100;

			frog.update(sideCollisions, verticalCollisions, fliesToEat);
			healthbar.update(frog.hunger, frog.breath, (int)(frog.jumpCharge*100)-40);
			draw.update(((int)frog.x+(frog.WIDTH+frog.BLANK_LEFT)/2)*-1+WIDTH/2, ((int)frog.y+(frog.HEIGHT+frog.BLANK_TOP)/2)*-1+HEIGHT/2);
			sideCollisions=generator.getSideCollisions();
			verticalCollisions=generator.getVerticalCollisions();

			if (frog.hunger<=0) {
				room=3;
				counter=0;
				lastScore=score;
				lastTimeAlive=60-time;
				score=0;
				time=60;
				frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
				generator=new Generator(WATER_LEVEL);
				healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
			}
			if (frog.breath<=0) {
				room=4;
				counter=0;
				lastScore=score;
				lastTimeAlive=60-time;
				score=0;
				time=60;
				frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
				generator=new Generator(WATER_LEVEL);
				healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
			}
			if (time<=0) {
				room=5;
				counter=0;
				lastScore=score;
				lastTimeAlive=60-time;
				score=0;
				time=60;
				frog=new Frog(0, 1632, 0.01, .98, .94, .94, WATER_LEVEL, keyboard);
				generator=new Generator(WATER_LEVEL);
				healthbar=new Healthbar(frog.MAX_HUNGER, frog.MAX_BREATH, (int)(frog.MAX_JUMP_CHARGE*100)-40);
			}
		}

		if (room==3) {
			keyboard.update();
			if (keyboard.keysPressed[10]) {
				room=1;
			}
		}
		if (room==4) {
			keyboard.update();
			if (keyboard.keysPressed[10]) {
				room=1;
			}
		}
		if (room==5) {
			keyboard.update();
			if (keyboard.keysPressed[10]) {
				room=1;
			}
		}
		if (room==6) {
			keyboard.update();
			if (keyboard.keysPressed[10]) {
				room=1;
			}
		}
	}

	public void run() {
		drawArea.setSize(new Dimension(getWidth(), getHeight()));
		add(drawArea);
		drawArea.createBufferStrategy(3);
		bufferStrategy=drawArea.getBufferStrategy();
		drawArea.addKeyListener(keyboard);
		System.out.println("HERE");
		drawArea.requestFocus();

		while (running) {
			currentNanoTime=System.nanoTime();
			while (currentNanoTime>lastNanoTime+TIME_BETWEEN_UPDATES) {
				lastNanoTime+=TIME_BETWEEN_UPDATES;
				update();
				updates++;
			}
			render();
			frames++;

			if (System.currentTimeMillis()-lastMilliTime>1000) {
				lastMilliTime+=1000;
				System.out.println("FPS: "+frames+"   Updates: "+updates);
				frames=0;
				updates=0;

			}
		}
	}
}
